package com.nodusk.controller;

import com.google.common.base.CaseFormat;
import com.nodusk.model.Db;
import com.nodusk.model.Plugin;
import com.nodusk.model.Response;
import com.nodusk.model.TableClass;
import com.nodusk.util.DbUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class DbController {
    @PostMapping("/connect")
    public Response connect(@RequestBody Db db) {
        Connection connection = DbUtils.init(db);
        if (connection != null) {
            return Response.ok("数据库连接成功");
        }
        return Response.error("数据库连接失败");
    }

    @PostMapping("/config")
    public Response config(@RequestBody Map<String, String> map) {
        String packageName = map.get("packageName");

        boolean enableLombok = Boolean.parseBoolean(map.get("enableLombok"));
        boolean enableSwagger =  Boolean.parseBoolean(map.get("enableSwagger"));
        boolean enableMybatisplus = Boolean.parseBoolean( map.get("enableMybatisplus"));
        boolean enableCustomAnnotation = Boolean.parseBoolean( map.get("enableCustomAnnotation"));
        try {
            Plugin plugin=new Plugin(enableLombok,enableSwagger,enableMybatisplus,enableCustomAnnotation);
            Connection connection = DbUtils.getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet tables = metaData.getTables(connection.getCatalog(), null, null, null);
            List<TableClass> tableList = new ArrayList<>();
            while (tables.next()) {
                TableClass tableClass = new TableClass();
                tableClass.setPackageName(packageName);
                String table_name = tables.getString("TABLE_NAME");
                String remarks = tables.getString("REMARKS");

                //转驼峰
                String modelName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, table_name);
                StringBuilder abbreviationName=new StringBuilder();

               abbreviationName.append(modelName.chars()
                        .filter((c) -> Character.isUpperCase(c))
                        .collect(StringBuilder::new, // supplier
                                StringBuilder::appendCodePoint, // accumulator
                                StringBuilder::append) // combiner
                        .toString().toLowerCase())  ;
               if(abbreviationName.length()<=1){
                   abbreviationName=new StringBuilder(modelName.toLowerCase());
               }
                tableClass.setAbbreviationName(abbreviationName.toString());
                tableClass.setTableName(table_name);
                tableClass.setRemarksTable(remarks);
                tableClass.setModelName(modelName);
                tableClass.setControllerName(modelName + "Controller");
                tableClass.setMapperName(modelName + "Mapper");
                tableClass.setServiceName(modelName + "Service");
                tableClass.setServiceImplName(modelName + "ServiceImpl");
                tableList.add(tableClass);
                tableClass.setPlugin(plugin);
            }
            return Response.ok("数据库信息读取成功", tableList);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return Response.error("数据库信息读取失败");
    }
}
